/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.plugin.portunis.impl;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.KeyStoreException;
import java.security.cert.Certificate;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.desktop.plugin.portunis.ZoomKeyStore;
import nu.zoom.swing.layout.VerticalPanel;

import org.ops4j.gaderian.Messages;

/**
 * A dialog to let the user confirm that a certificate should be impoerted to a
 * keystore.
 * 
 * @version $Revision: 1.1 $
 * @author $Author: johan $
 */
public class ImportCertificateDialog extends JDialog {
    private static final long serialVersionUID = -1;
    private final Workbench workbench;
    private final ZoomKeyStore store;
    private final Messages messages;
    private final JTextField aliasField;
    private boolean closeOK = false;

    public ImportCertificateDialog(Workbench workbench, Messages messages,
            Certificate cert, ZoomKeyStore store) {
        super(workbench.getDialogOwner(), messages.getMessage("keystore.import.cert.confirm.title"), true);
        this.messages = messages;
        this.store = store;
        this.workbench = workbench;

        CertificateComponentFactory factory = new CertificateComponentFactory(
                workbench, messages);
        JComponent certComponent = factory.getCertificateComponent(cert);
        JPanel buttonPanel = new JPanel();
        JButton cancelButton = new JButton(messages.getMessage("keystore.general.cancel"));
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                closeOK = false;
                dispose();
            }
        });
        JButton okButton = new JButton(messages.getMessage("keystore.import.cert.confirm.ok"));
        okButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (validateFields()) {
                    closeOK = true;
                    dispose();
                }
            }
        });

        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);

        VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.addRow(new JLabel(messages.getMessage("keystore.import.cert.confirm.message")));
        aliasField = new JTextField(20);
        mainPanel.addRow(new JLabel(messages.getMessage("keystore.import.cert.alias")), aliasField);
        JScrollPane certScroller = new JScrollPane(certComponent);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(mainPanel, BorderLayout.NORTH);
        getContentPane().add(certScroller, BorderLayout.CENTER);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }

    private boolean validateFields() {
        String alias = aliasField.getText();
        if ((alias == null) || (alias.length() < 1)) {
            JOptionPane.showMessageDialog(this, messages.getMessage("keystore.import.cert.alias.toshort"));
            return false;
        }
        try {
            if (store.containsAlias(alias)) {
                int option = JOptionPane.showConfirmDialog(
                        this,
                        messages.getMessage("keystore.import.cert.alias.exists"),
                        messages.getMessage("keystore.import.cert.alias.exists.title"),
                        JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (KeyStoreException e) {
            workbench.getErrorReporter().reportError(e);
        }

        return true;
    }

    public boolean isCloseOK() {
        return closeOK;
    }

    public String getAlias() {
        return aliasField.getText();
    }
}
